package com.dede.applib.recycleview

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.View

/**
 * @author hsh
 * @date 2017/9/6 14:35.
 * @doc RecyclerView分割线，可以自定义颜色，高度。适用于GridLayoutManager，StaggeredGridLayoutManager，LinearLayoutManager
 */
class ItemColorDecoration(private val decorationHeight: Int, private val color: Int = Color.TRANSPARENT) : RecyclerView.ItemDecoration() {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)

    init {
        paint.color = color
    }

    fun setColor(color: Int) {
        paint.color = color
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State?) {
        super.onDraw(c, parent, state)
        if (paint.color == Color.TRANSPARENT)
            return

        val layoutManager = parent.layoutManager

        if (layoutManager is GridLayoutManager || layoutManager is StaggeredGridLayoutManager) {
            for (i in 0..parent.childCount - 1) {
                val child = parent.getChildAt(i)
                val layoutParams = child.layoutParams as RecyclerView.LayoutParams
                //绘制横向分割线
                var left = child.left
                var top = child.bottom + layoutParams.bottomMargin
                var right = child.right
                var bottom = top + decorationHeight
                c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint)

                //绘制纵向分割线
                left = child.right + layoutParams.rightMargin
                top = child.top
                right = left + decorationHeight
                bottom = child.measuredHeight + top + decorationHeight
                c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint)
            }
        } else if (layoutManager is LinearLayoutManager) {
            when (getOrientation(layoutManager)) {
                LinearLayoutManager.VERTICAL -> {
                    val left = parent.paddingLeft
                    val right = parent.measuredWidth - parent.paddingRight
                    for (i in 0..parent.childCount - 1) {
                        val child = parent.getChildAt(i)
                        val layoutParams = child.layoutParams as RecyclerView.LayoutParams
                        val top = child.bottom + layoutParams.bottomMargin
                        val bottom = top + decorationHeight
                        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint)
                    }
                }
                LinearLayoutManager.HORIZONTAL -> {
                    val top = parent.paddingTop
                    for (i in 0..parent.childCount - 1) {
                        val child = parent.getChildAt(i)
                        val layoutParams = child.layoutParams as RecyclerView.LayoutParams
                        val left = child.right + layoutParams.rightMargin
                        val right = left + decorationHeight
                        val bottom = child.measuredHeight + top
                        c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), paint)
                    }
                }
            }
        }

    }

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State?) {
        super.getItemOffsets(outRect, view, parent, state)

        val layoutManager = parent.layoutManager

        if (layoutManager is GridLayoutManager || layoutManager is StaggeredGridLayoutManager) {
            val spanCount = getSpanCount(layoutManager)//列数
            val childPosition = parent.getChildAdapterPosition(view)
            val lastLine = layoutManager.itemCount % spanCount//最后一行或列剩余个数
            when (getOrientation(layoutManager)) {
                StaggeredGridLayoutManager.VERTICAL,
                GridLayoutManager.VERTICAL -> {
                    if (childPosition % spanCount != spanCount - 1) {//最后一列右边不添加偏移量
                        outRect.right = decorationHeight
                    }
                    if (lastLine != 0) {
                        if (layoutManager.itemCount - childPosition > lastLine)//最后一行底部不添加偏移量
                            outRect.bottom = decorationHeight
                    } else {//最后一行没有空位
                        if (layoutManager.itemCount - childPosition > spanCount)//最后一行底部不添加偏移量
                            outRect.bottom = decorationHeight
                    }
                }
                StaggeredGridLayoutManager.HORIZONTAL,
                GridLayoutManager.HORIZONTAL -> {
                    if (childPosition % spanCount != spanCount - 1) {//最后一行底部不添加偏移量
                        outRect.bottom = decorationHeight
                    }
                    if (lastLine != 0) {
                        if (layoutManager.itemCount - childPosition > lastLine)//最后一列右边不添加偏移量
                            outRect.right = decorationHeight
                    } else {//最后一列没有空位
                        if (layoutManager.itemCount - childPosition > spanCount)//最后一列右边不添加偏移量
                            outRect.right = decorationHeight
                    }
                }
            }
        } else if (layoutManager is LinearLayoutManager) {
            when (getOrientation(layoutManager)) {
                LinearLayoutManager.VERTICAL -> {
                    val layoutPosition = parent.getChildLayoutPosition(view)
                    if (layoutPosition != layoutManager.itemCount - 1)//最后条目不底部添加偏移量
                        outRect.bottom = decorationHeight
                }
                LinearLayoutManager.HORIZONTAL -> {
                    val layoutPosition = parent.getChildLayoutPosition(view)
                    if (layoutPosition != layoutManager.itemCount - 1)
                        outRect.right = decorationHeight//最后条目不右边添加偏移量
                }
            }
        }
    }

    /**
     * 获取方向
     *
     * @param layoutManager
     * @return
     */
    private fun getOrientation(layoutManager: RecyclerView.LayoutManager): Int {
        var orientation = LinearLayoutManager.VERTICAL
        when (layoutManager) {
            is GridLayoutManager -> orientation = layoutManager.orientation
            is StaggeredGridLayoutManager -> orientation = layoutManager.orientation
            is LinearLayoutManager -> orientation = layoutManager.orientation
        }
        return orientation
    }

    /**
     * 获取列数
     *
     * @param layoutManager
     * @return
     */
    private fun getSpanCount(layoutManager: RecyclerView.LayoutManager): Int {
        var spanCount = 0
        when (layoutManager) {
            is GridLayoutManager -> spanCount = layoutManager.spanCount
            is StaggeredGridLayoutManager -> spanCount = layoutManager.spanCount
            is LinearLayoutManager -> spanCount = 1
        }
        return spanCount
    }
}
